package com.yunda.nebula.gateway.sync.nacos;

import com.yunda.nebula.gateway.core.DataChangedListener;
import com.yunda.nebula.gateway.core.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhiguo.dong
 * @date 2020/9/23
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = NacosConfig.PREFIX,name = "server-address")
public class NacosSyncDataConfiguration {


    @Bean
    public SyncDataService nacosSyncDataService(NacosConfig config, final ObjectProvider<DataChangedListener> dataChangedListeners) {
        log.info("you use http long pull sync gateway data");
        DataChangedListener dataChangedListener = dataChangedListeners.getIfAvailable();
        NacosSyncDataService syncDataService = new NacosSyncDataService(config,dataChangedListener);
        return syncDataService;
    }

    @Bean
    @ConfigurationProperties(prefix = NacosConfig.PREFIX)
    public NacosConfig nacosConfig(){
        return new NacosConfig();
    }
}
